package com.itheima.doudizhu;

import java.util.*;

/**
 * @author zhangbaiyan
 * @date 2024-09-13 16:02
 * @description: 练习3
 * @version: 1.0
 */
public class pokerGame3 {
    //  静态代码块
//  特点:随着类的加载而加载的,而却只只进行一次
    static ArrayList<String> list = new ArrayList<>();
    //    创建一个集合用来添加牌的价值
    static HashMap<String, Integer> hm = new HashMap<>();

    static {
        //准备牌
        // "♦',"♣","♥","♠"
        // "A","2","3","4","5","6","7","8","9","10","J","Q","K"
        String[] colors = {"♦", "♣", "♥", "♠"};
        String[] numbers = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};

        //牌盒

        for (String color : colors) {
            for (String number : numbers) {
                list.add(color + number);
            }
        }
        list.add(" 大王");
        list.add(" 小王");
        //指定牌的价值
        //牌上的书瞎子到map集合中判断是否存在
        //存在,获取价值
        //不存在,本身的数字就是价值
        hm.put("J", 11);
        hm.put("Q", 12);
        hm.put("K", 13);
        hm.put("A", 14);
        hm.put("2", 15);
        hm.put("小王", 50);
        hm.put("大王", 100);
    }

    public pokerGame3() {
        //洗牌
        Collections.shuffle(list);
        //发牌
        ArrayList<String> player1 = new ArrayList<>();
        ArrayList<String> player2 = new ArrayList<>();
        ArrayList<String> player3 = new ArrayList<>();
        ArrayList<String> lord = new ArrayList<>();
        //遍历派和得到每一张牌
        for (int i = 0; i < list.size(); i++) {
            //i:索引
            String poker = list.get(i);
            if (i <= 2) {
                lord.add(poker);
                continue;
            }
            //给三个玩家轮流发牌
            if (i % 3 == 0) {
                player1.add(poker);
            } else if (i % 3 == 1) {
                player2.add(poker);
            } else {
                player3.add(poker);
            }
        }
        //看牌


        order(player2);
        order(player3);
        order(lord);
        lookPoker("底牌", lord);
        lookPoker("玩家1", player2);
    }
    public void lookPoker(String name, ArrayList<String> list) {
        System.out.print(name + "的牌是:");
        //遍历TreeSet 集合得到每一个序号,再拿着序号到Map集合中去找真正的牌
        for (String i : list) {

            System.out.print(i + " ");
        }
        System.out.println();
    }

    //利用牌的价值进行排序
    //参数:集合
    public void order(ArrayList<String> list) {
        Collections.sort(list, new Comparator<String>() {
            //Array.sort(插入排序+二分查找)
            @Override
            public int compare(String o1, String o2) {
                //o1:表示单签要插入到有序序列中的牌
                //o2:表示已经再有序序列中存在的牌

                //负数:o1小插入到前面
                //正数:o1大插入到后面
                //0:o1的数字跟o2的数字是一样的,需要按照花色再次排序

                //1.计算o1的花色社价值 ♥3
                String o1Color = o1.substring(0, 1);
                int o1Value = getValue(o1);
                //1.计算o1的花色社价值 ♥3
                String o2Color = o2.substring(0, 1);
                int o2Value = getValue(o2);
                //3.比较o1和o2价值
                int i=o1Value - o2Value;

                return i==0?o1Color.compareTo(o2Color):i;
            }
        });

    }

    //计算牌的价值
    //参数:牌
    //返回值:价值
    public int getValue(String poker) {
        //获取牌上的数字
        String number = poker.substring(1);
        //拿到数字到map集合中判断是否存在
        //存在,获取价值
        //不存在类型转换
        if (hm.containsKey(number)) {
            //存在,获取价值
            return hm.get(number);
        } else {
            //不存在类型转换
            return Integer.parseInt(number);
        }
    }

}

